<?php
require_once ("cfginicial.php");
require_once(INCLUDE_DIR."/DBSetup.inc.php");
require_once (INCLUDE_DIR."/funciones_generales.php");
require_once (INCLUDE_DIR."/funciones_menues.php");
require_once (INCLUDE_DIR."/funciones_sesion.php");
require_once (INCLUDE_DIR."/funciones_fechas.php");

## +---------------------------------------------------------------------------+
## | 1. Creating & Calling:                                                    | 
## +---------------------------------------------------------------------------+
##  *** define a relative (virtual) path to datagrid.class.php file and "pear" 
##  *** directory (relatively to the current file)
##  *** RELATIVE PATH ONLY ***
  define ("DATAGRID_DIR", "datagrid_4.2.8/");
  define ("PEAR_DIR", "datagrid_4.2.8/pear/");
  
  require_once(DATAGRID_DIR.'datagrid.class.php');
  require_once(PEAR_DIR.'PEAR.php');
  require_once(PEAR_DIR.'DB.php');
##
##  *** creating variables that we need for database connection 
  $DB_USER='admin';            
  $DB_PASS='admin';           
  $DB_HOST='localhost:5432';       
  $DB_NAME='cgpq_dev';    

  ob_start();
##  *** (example of ODBC connection string)
##  *** $result_conn = $db_conn->connect(DB::parseDSN('odbc://root:12345@test_db'));
##  *** (example of Oracle connection string)
##  *** $result_conn = $db_conn->connect(DB::parseDSN('oci8://root:12345@localhost:1521/mydatabase)); 
##  *** (example of PostgreSQL connection string)
##  *** $result_conn = $db_conn->connect(DB::parseDSN('pgsql://root:12345@localhost/mydatabase)); 
##  === (Examples of connections to other db types see in "docs/pear/" folder)

  $db_conn = DB::factory('pgsql');  /* don't forget to change on appropriate db type */
  $result_conn = $db_conn->connect(DB::parseDSN('pgsql://'.$DB_USER.':'.$DB_PASS.'@'.$DB_HOST.'/'.$DB_NAME));
  if(DB::isError($result_conn)){ die($result_conn->getDebugInfo()); }  
##  *** put a primary key on the first place 
  $sql=" SELECT "
   ."demo_countries.id, "
   ."demo_countries.region_id, "
   ."demo_regions.name as region_name, "
   ."demo_countries.name as country_name, "
   ."demo_countries.description, "
   ."demo_countries.picture_url, "
   ."demo_countries.independent_date, "
   ."demo_countries.independent_time, "
   ."FORMAT(demo_countries.population, 0) as population, "   
   ."(SELECT COUNT(demo_presidents.id) FROM demo_presidents WHERE demo_presidents.country_id = demo_countries.id) as presidents, "
   ." CASE WHEN demo_countries.is_democracy = 1 THEN 'Yes' ELSE 'No' END as is_democracy "
   ."FROM demo_countries INNER JOIN demo_regions ON demo_countries.region_id=demo_regions.id ";

	$sql = "SELECT "
	."t_tipo_cia.id, "
	."t_tipo_cia.descripcion, "
	."t_tipo_cia.nivel_autoridad, "
	."t_tipo_cia.limite, "
	." CASE WHEN t_tipo_cia.flag_habilitado = 'S' THEN 'Ok' ELSE 'No' END  as habilitado "
	."FROM t_tipo_cia " ;
	
/*	
	id bigint NOT NULL,
    descripcion character varying(100) NOT NULL,
    nivel_autoridad integer NOT NULL,
    limite integer NOT NULL,
    flag_habilitado character(1) NOT NULL
*/

##  *** set needed options and create a new class instance 
  $debug_mode = false;        /* display SQL statements while processing */    
  $messaging = true;          /* display system messages on a screen */ 
  $unique_prefix = "f_";    /* prevent overlays - must be started with a letter */
  $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
##  *** set data source with needed options
  $default_order_field = "id";
  $default_order_type = "ASC";
  $dgrid->dataSource($db_conn, $sql, $default_order_field, $default_order_type);	    
##

	$dgrid->draw_add_button_separately = true;
	$dgrid->SetHeadersInColumnarLayout("Atributo","Valor");
##
## +---------------------------------------------------------------------------+
## | 2. General Settings:                                                      | 
## +---------------------------------------------------------------------------+
##  *** set encoding and collation (default: utf8/utf8_unicode_ci)
 $dg_encoding = "utf-8";
 $dg_collation = "utf8_unicode_ci";
 $dgrid->setEncoding($dg_encoding, $dg_collation);
##  *** set interface language (default - English)
##  *** (en) - English     (de) - German     (se) Swedish     (hr) - Bosnian/Croatian
##  *** (hu) - Hungarian   (es) - Espanol    (ca) - Catala    (fr) - Francais
##  *** (nl) - Netherlands/"Vlaams"(Flemish) (it) - Italiano  (pl) - Polish
##  *** (ch) - Chinese     (sr) - Serbian
 $dg_language = "es";  
 $dgrid->setInterfaceLang($dg_language);

// $dgrid->setTemplates("","",$details_template);
##  *** set modes for operations ("type" = "link|button|image"),
##  *** "view" - view mode | "edit" - add/edit/details modes
##  *** ("byFieldValue" - make the field as a link to edit mode page)

$modes = array(
  "add"     =>array("view"=>false, "edit"=>false, "type"=>"button","show_add_button"=>"outside"),
  "edit"    =>array("view"=>false, "edit"=>true, "type"=>"link",  "byFieldValue"=>""),
  "cancel"  =>array("view"=>true, "edit"=>true, "type"=>"link"),
  "details" =>array("view"=>true, "edit"=>false, "type"=>"link"),
  "delete"  =>array("view"=>false, "edit"=>true, "type"=>"link"),
  "mimodo"  =>array("view"=>true, "edit"=>true, "type"=>"link")

);
$dgrid->SetModes($modes);

$multirow_option = true;
$dgrid->AllowMultirowOperations($multirow_option);

$multirow_operations = array(
  "delete"  => array("view"=>false),
  "details" => array("view"=>false),
  "my_operation_name" => 
     array("view"=>true, 
           "flag_name"=>"my_flag_name", 
           "flag_value"=>"my_flag_value", 
           "tooltip"=>"Do something with selected", 
           "image"=>"../../../images/bg_boton_azul.jpg")
);
$dgrid->SetMultirowOperations($multirow_operations);  
##
## +---------------------------------------------------------------------------+
## | 3. Printing & Exporting Settings:                                         | 
## +---------------------------------------------------------------------------+
##  *** set printing option: true(default) or false 
 $printing_option = true;
 $dgrid->allowPrinting($printing_option);
##  *** set exporting option: true(default) or false 
 $exporting_option = true;
 $dgrid->allowExporting($exporting_option);
##
##
## +---------------------------------------------------------------------------+
## | 4. Sorting & Paging Settings:                                             | 
## +---------------------------------------------------------------------------+
##  *** set sorting option: true(default) or false 
 $sorting_option = true;
 $dgrid->allowSorting($sorting_option);               
##  *** set paging option: true(default) or false 
 $paging_option = true;
 $rows_numeration = false;
 $numeration_sign = "N #";       
 $dgrid->allowPaging($paging_option, $rows_numeration, $numeration_sign);
##  *** set paging settings
 $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
 $top_paging = array();
 $pages_array = array("1"=>"1","10"=>"10", "25"=>"25", "50"=>"50");
 $default_page_size = 10;
 $dgrid->setPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
##
##
## +---------------------------------------------------------------------------+
## | 5. Filter Settings:                                                       | 
## +---------------------------------------------------------------------------+
##  *** set filtering option: true or false(default)
 $filtering_option = true;
 $dgrid->allowFiltering($filtering_option);

$filtering_fields = array(
   "Nivel Autoridad"=>array(
        "type"=>"dropdownlist",
        "table"=>"t_tipo_cia",
		"source" => "self",
        "field"=>"nivel_autoridad",
        "show_operator"=>true,
        "comparison_type"=>"numeric", 
        "width"=>"160px",
        "on_js_event"=>""), 

   "Habilitado"=>array(
        "type"=>"dropdownlist",
        "table"=>"t_tipo_cia",
	"source" => array("S"=>"Si", "N"=> "No"),
        "field"=>"flag_habilitado",
        "show_operator"=>false,
        "case_sensitive"=>false,
        "comparison_type"=>"string", 
        "width"=>"160px",
        "on_js_event"=>"")
	);
$dgrid->SetFieldsFiltering($filtering_fields);

##
## 
## +---------------------------------------------------------------------------+
## | 6. View Mode Settings:                                                    | 
## +---------------------------------------------------------------------------+
##  *** set view mode table properties
/// $vm_table_properties = array("width"=>"90%");
/// $dgrid->setViewModeTableProperties($vm_table_properties);  
##  *** set columns in view mode
##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
 $vm_colimns = array(
    "descripcion"  =>array("header"=>"Descripcion",      
				"type"=>"label", 
				"width"=>"130px", 
				"align"=>"left",   
				"wrap"=>"nowrap", 
				"text_length"=>"-1", "case"=>"normal"),
		"nivel_autoridad"   =>array("header"=>"Nivel Autoridad",  
		"type"=>"label",  "align"=>"center",  "width"=>"50px",
		"wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
		"limite"					=> array("header"=>"Limite",  "type"=>"label",  "align"=>"center",  "width"=>"40px","wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
	"habilitado" 		=>array("header"=>"Habilitado",     
					"type"=>"label", "align"=>"center", 
					"width"=>"130px", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal",  "on_js_event"=>""),
    //"population"   =>array("header"=>"Population",       "type"=>"label", "summarize"=>true, "align"=>"right",  "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
    //"presidents"   =>array("header"=>"Presidents",       "type"=>"label", "summarize"=>true, "align"=>"right",  "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
    //"description"  =>array("header"=>"Short Description","type"=>"label", "align"=>"left",   "wrap"=>"wrap",   "text_length"=>"15", "case"=>"lower"),
//    "picture_url"  =>array("header"=>"Picture",          "type"=>"image", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal", "summarize"=>false, "on_js_event"=>"", "target_path"=>"uploads/", "default"=>"", "image_width"=>"17px", "image_height"=>"17px"),
  );
  $dgrid->setColumnsInViewMode($vm_colimns);
##
##
## +---------------------------------------------------------------------------+
## | 7. Add/Edit/Details Mode Settings:                                        | 
## +---------------------------------------------------------------------------+
##  *** set add/edit mode table properties
/// $em_table_properties = array("width"=>"70%");
/// $dgrid->setEditModeTableProperties($em_table_properties);
##  *** set details mode table properties
/// $dm_table_properties = array("width"=>"70%");
/// $dgrid->setDetailsModeTableProperties($dm_table_properties);
##  ***  set settings for add/edit/details modes
  $table_name  = "t_tipo_cia";
  $primary_key = "id";
  $condition   = "";
  $dgrid->setTableEdit($table_name, $primary_key, $condition);
##  *** set columns in edit mode
##  *** first letter: r - required, s - simple (not required)
##  *** second letter: t - text(including datetime), n - numeric, a - alphanumeric, e - email, f - float, y - any, l - login name, z - zipcode, p - password, i - integer, v - verified
##  *** third letter (optional): 
##          for numbers: s - signed, u - unsigned, p - positive, n - negative
##          for strings: u - upper,  l - lower,    n - normal,   y - any
##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
##  *** Ex.: type = textbox|textarea|label|date(yyyy-mm-dd)|datedmy(dd-mm-yyyy)|datetime(yyyy-mm-dd hh:mm:ss)|datetimedmy(dd-mm-yyyy hh:mm:ss)|image|password|enum|print|checkbox
##  *** make sure your WYSIWYG dir has 755 permissions


  $em_columns = array(
//    "region_id"        =>array("header"=>"Region",           "type"=>"textbox",  "width"=>"210px", "req_type"=>"rt", "title"=>"Region Name"),
//    "name"             =>array("header"=>"Country",          "type"=>"textbox",  "width"=>"210px", "req_type"=>"ry", "title"=>"Country Name", "unique"=>true),
    "descripcion"       =>array("header"=>"Descripcion",  
		"type"=>"textbox", "width"=>"210px", 
//		"req_type"=>"sty", 
		"title"=>"Descripcion",
		 "on_js_event"=>"onclick='alert(\"Yes!!!\");'"),
    "nivel_autoridad"   =>array("header"=>"Nivel Autoridad",
			"req_type"=>"rnp",
			"type"=> "textbox",
			"view_type"=>"textbox",
			"width"=>"210px", 
			"title"=>"Nivel de Autoridad" ),
    "limite"      	=>array("header"=>"Limite",     
			"type"=>"textbox",  
			),
    "flag_habilitado"   =>array("header"=>"Habilitado",     
			"type"=>"enum", 
			"source"=> array("S"=>"Si","N"=>"No" ) ,
			"view_type"=>"dropdownlist","width"=>"210px", "title"=>"Limite Itulo" ),
    


//    "population"       =>array("header"=>"Peoples",          "type"=>"enum",     "source"=>$fill_from_array, "view_type"=>"dropdownlist",  "width"=>"139px", "req_type"=>"ri", "title"=>"Population (Peoples)"),
//    "picture_url"      =>array("header"=>"Image URL",        "type"=>"image",    "req_type"=>"st", "width"=>"210px", "title"=>"Picture", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"100K", "image_width"=>"100px", "image_height"=>"100px", "file_name"=>"", "host"=>"local"),
//    "is_democracy"     =>array("header"=>"Is Democracy",     "type"=>"checkbox", "true_value"=>1, "false_value"=>0,  "width"=>"210px", "req_type"=>"sy", "title"=>"Is Democraty"),
//    "independent_date" =>array("header"=>"Independence Day", "type"=>"date",     "width"=>"210px", "req_type"=>"rt", "title"=>"Independence Day"),
//    "independent_time" =>array("header"=>"Independence Time", "type"=>"time",     "width"=>"210px", "req_type"=>"rt", "title"=>"Independence Time")
  );
  $dgrid->setColumnsInEditMode($em_columns);
##  *** set auto-genereted eName_1.FieldName > 'a' AND TableName_1.FieldName < 'c'"
##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
// $foreign_keys = array(
//    "region_id"=>array("table"=>"demo_regions", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"name", "order_type"=>"ASC")
// ); 
// $dgrid->setForeignKeysEdit($foreign_keys);
##
##
## +---------------------------------------------------------------------------+
## | 8. Bind the DataGrid:                                                     | 
## +---------------------------------------------------------------------------+
##  *** bind the DataGrid and draw it on the screen


	fMostrarEncabezadoPagina($i18n->getKey("menu.tipocia_ver"));         
        
	$dgrid->bind();        

   fMostrarPiePagina();

  ob_end_flush();
    
    

?>
</body>
